package com.swy.shopgroup.view;

import androidx.appcompat.app.AppCompatActivity;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.text.method.HideReturnsTransformationMethod;
import android.text.method.PasswordTransformationMethod;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.swy.shopgroup.MainActivity;
import com.swy.shopgroup.MainActivity2;
import com.swy.shopgroup.MainActivity3;
import com.swy.shopgroup.R;
import com.swy.shopgroup.Utils;
import com.swy.shopgroup.entity.ShopUser;
import com.swy.shopgroup.network.OkHttp;

import java.io.IOException;

import io.reactivex.Observable;
import io.reactivex.ObservableEmitter;
import io.reactivex.ObservableOnSubscribe;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;
import okhttp3.Response;

public class LoginActivity extends AppCompatActivity {
    private ImageView passVis;
    private EditText code;
    private EditText phoneNum;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        if (Utils.getUserToken(this) != null) {
            switch (Utils.getUserToken(this).getData().getType()) {
                case 0:
                    Intent intent = new Intent(LoginActivity.this, MainActivity3.class);
                    startActivity(intent);
                    finish();
                    break;
                case 1:
                    intent = new Intent(this, MainActivity.class);
                    startActivity(intent);
                    finish();
                    break;
                case 2:
                    intent = new Intent(LoginActivity.this, MainActivity2.class);
                    startActivity(intent);
                    finish();
                    break;
                default:
                    break;
            }
        }
        initView();
        initData();
    }

    public void initView() {
        passVis = findViewById(R.id.passVis);
        code = findViewById(R.id.code);
        phoneNum = findViewById(R.id.phoneNum);
    }

    public void initData() {

    }

    @SuppressLint("CheckResult")
    public void login(final String name, final String pwd) {
        Observable.create(new ObservableOnSubscribe<String>() {
            @Override
            public void subscribe(ObservableEmitter<String> e) throws Exception {
                OkHttp o = new OkHttp();
                try {
                    Response response = o.login(name, pwd);
                    e.onNext(response.body().string());
                } catch (IOException a) {
                    a.printStackTrace();
                    e.onNext("s");
                }
            }
        }).subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribe(new io.reactivex.functions.Consumer<String>() {
            @Override
            public void accept(@io.reactivex.annotations.NonNull String s) throws Exception {
                try {
                    Gson gson = new Gson();
                    ShopUser result = gson.fromJson(s, ShopUser.class);
                    if (result.getCode().equals("200")) {
                        Utils.setUserToken(LoginActivity.this, s);
                        if (result.getData().getType() == 1) {
                            Intent intent = new Intent(LoginActivity.this, MainActivity.class);
                            startActivity(intent);
                        } else if (result.getData().getType() == 2) {
                            Intent intent = new Intent(LoginActivity.this, MainActivity2.class);
                            startActivity(intent);
                        } else {
                            Intent intent = new Intent(LoginActivity.this, MainActivity3.class);
                            startActivity(intent);
                        }
                        finish();
                    }
                    Toast.makeText(LoginActivity.this, result.getMsg(), Toast.LENGTH_SHORT).show();
                } catch (Exception e) {
                }
            }
        });
    }

    public void click(View v) {
        switch (v.getId()) {
            case R.id.login:
                String s = phoneNum.getText().toString();
                String s1 = code.getText().toString();
                if (TextUtils.isEmpty(s)) {
                    Toast.makeText(this, "请输入账号", Toast.LENGTH_SHORT).show();
                    return;
                }
                if (TextUtils.isEmpty(s1)) {
                    Toast.makeText(this, "请输入密码", Toast.LENGTH_SHORT).show();
                    return;
                }
                login(s, s1);
                break;
            case R.id.register:
                Intent intent = new Intent(this, RegisterActivity.class);
                startActivity(intent);
                finish();
                break;
            case R.id.passVis:
                String msg = code.getText().toString();
                if (passVis.isSelected()) {
                    code.setTransformationMethod(PasswordTransformationMethod.getInstance());
                } else {
                    code.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
                }
                passVis.setSelected(!passVis.isSelected());
                code.setSelection(msg.length());
                break;
            case R.id.mFinish:
                finish();
                break;
            default:
                break;
        }
    }
}